<?php

	/**
	 * Defines a class to be used for MySQL databases
	 * 
	 * @author Pablo Santiago Sánchez <phackwer@gmail.com>
	 * @copyright Copyright (c) 2008, Pablo Santiago Sánchez
	 * @license http://opensource.org/licenses/bsd-license.php BSD License
	 * @package pop
	 * @subpackage drivers
	 */

	/**
	 * Defines a class to be used for MySQL databases
	 * 
	 * @package pop
	 * @subpackage drivers
	 */
	class PMySQLDBDriver extends PGenericDBDriver
	{
		public $schema = '';
		public $separator = '_';
		public $driver = 'mysql';
		
		/**
		 * Class constructor
		 * @param $pdodb
		 * @return void
		 */
		public function __construct()
		{		
		}
		
		public function newConnection($server,$dbname,$user,$password)
		{
			$conn = new PDO($this->driver.':host='.$server.';dbname='.$dbname,$user,$password);
			$conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
			return $conn;
		}
		
		/**
		 * Returns the last inserted itme id
		 * @param $pdodb
		 * @param $table
		 * @param $idname
		 * @return integer
		 */
		public function getLastId($pdodb, $table, $idname)
		{
			return POPDB::getConnection($pdodb)->lastInsertId();
		}

		/**
		 * Returns the required string for the createTable method
		 * @return string
		 */
		public function identityField()
		{
			return " integer not null auto_increment ";
		}
		
		/**
		 * Creates the required schema to avoid errors on creating tables
		 * @param $schema
		 * @return void
		 */
		public function createSchema($pdodb, $schema)
		{
			//mysql doesn't work with schemas in a way that it's necessary
			//to create the schema, so, this method does nothing
		}

		/**
		 * Returns the required SQL for field creation
		 * @param $type
		 * @param $defaultval
		 * @param $size
		 * @return void
		 */
		public function fieldCreation($type, $defaultval, $size=null)
		{
			$sql_field = $type." ";

			if ($type=="varchar")
				$sql_field .= "(".$size.") ";

			if (is_null($defaultval))
				$sql_field .= "default null";
			else
			{
				if ($defaultval && $type!="datetime" && $type!="date" && $type!="time" && $type!="varchar" && $type!="text")
					$sql_field .= "default ".$defaultval;
				else if ($type=="varchar")
					$sql_field .= "default '".$defaultval."'";
			}
			
			return $sql_field;
		}

		/**
		 * Alter table
		 * @return void
		 */
		public function alterTable()
		{
		}

		/**
		 * Drops a table
		 * @param $pdodb
		 * @param $schema
		 * @param $table
		 * @return void
		 */
		public function dropTable($pdodb, $schema, $table)
		{
			$sql = "show tables;";
			$rs = POPDB::getConnection($pdodb)->query($sql);
			$result = $rs->fetchAll();
			$rs->closeCursor();
			POPDebug::log($sql);

			foreach ($result as $row)
				if($row[0]==$table)
				{
					$sql = "drop table ".$table.";\n";
					POPDB::getConnection($pdodb)->exec($sql);
					POPDebug::log($sql);
				}
		}
	}

	/** 
	 * Register the class for the driver
	 */
	POPDBDriverRegistry::addDriver("mysql", "PMySQLDBDriver");
?>